package com.ysd.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.entity.Modules;
import com.ysd.service.ModulesService;
import com.ysd.service.RolesService;
import com.ysd.util.Treemodule;

@RestController
@RequestMapping("/moduless")
@CrossOrigin
public class ModulesController {

	@Autowired
	private ModulesService modulesSer;
	@Autowired
	private RolesService rolesService;

	@GetMapping("/tree")
	public Map<Object, Object> findModulesTree() {
		Map<Object, Object> restMap = new HashMap<Object, Object>();

		List<Treemodule> tList = modulesSer.findModulesTree();
		restMap.put("data", tList);
		restMap.put("message", "成功");
		restMap.put("code", "0");
		return restMap;
	}

	@PutMapping("/modules")
	public Map<Object, Object> InsertOrUpdateModules(Modules modules, Integer pid) {
		Map<Object, Object> restMap = new HashMap<Object, Object>();

		// 判断是新增还是修改
		if (modules.getId() != null) {
			// 修改
			// 获取该模块原数据
			Modules m = modulesSer.findModulesById(modules.getId());
			// 修改原数据内容
			m.setName(modules.getName());
			m.setPath(modules.getPath());
			// 修改模块
			modules = modulesSer.insertOrUpdateModules(m);
			restMap.put("data", modules);
			restMap.put("code", "0");
			restMap.put("message", "模块修改成功");

		} else if (modules.getId() == null) {
			// 新增
			// 判断新增一级或其他
			if (pid != null) {
				// 新增其他级模块
				// 通过父ID获取父模块信息
				Modules parent = modulesSer.findModulesById(pid);
				modules.setParent(parent);
				// 新增模块信息
				modules = modulesSer.insertOrUpdateModules(modules);
				restMap.put("data", modules);
				restMap.put("code", "0");
				restMap.put("message", "子模块新增成功");

			} else {

			}

		}

		return restMap;
	}

	@DeleteMapping("/modules")
	public Map<Object, Object> delModulesById(Integer id) {
		Map<Object, Object> restMap = new HashMap<Object, Object>();
		boolean decide = true;

		// 获取所有模块信息
		List<Modules> mList = modulesSer.findAllModules();
		for (Modules m : mList) {
			if (m.getParent() != null) {
				if (((Modules) m.getParent()).getId().equals(id)) {
					decide = false;
					restMap.put("code", "1");
					restMap.put("message", "删除失败，该模块下有子模块存在");
				}
			}

		}

		if (decide) {
			Integer r = modulesSer.delModulesById(id);
			if (r > 0) {
				restMap.put("code", "0");
				restMap.put("message", "删除模块成功");
			} else {
				restMap.put("code", "1");
				restMap.put("message", "删除模块失败，未知错误");
			}
		}

		return restMap;
	}
	
	
	/**
	 * 
	 * @param roleid
	 * @param parentIds
	 * @return
	 */
	@RequestMapping("/SetSysRights")
	public Object SetSysRights(String Id,String parentIds) {
		System.out.println("我进入了角色权限设置");
		System.out.println( "parentIds"+parentIds);
		Map<String, Object> map= new HashMap<String, Object>();
		//String roleid = "f624bb7b-afc7-4bde-8c32-b2659eeba527";
		modulesSer.delModulesrole(Id);
		if(parentIds!=null) {
			System.out.println("parentIds"+parentIds);
			try {
				modulesSer.delModulesrole(Id);
			} catch (Exception e) {
				// TODO: handle exception
			}
			for(String id:parentIds.split(",")) {
				int mid = Integer.valueOf(id);
				modulesSer.saveModulesrole(Id, mid);
			}
			map.put("success", true);
		}else {
			System.out.println("未设置权限");
			map.put("success", false);
		}
		return map;
		
	}

	


}
